package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.UserBean;
import com.project.mapper.IUserMapper;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements IUserService {
    @Autowired
    private IUserMapper userMapper;
    @Override
    public UserBean login(String userName, String pwd) {
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        qw.eq("r_userName",userName);
        qw.eq("r_pwd",pwd);

        UserBean user = userMapper.selectOne(qw);

        return user;
    }

    @Override
    public List<UserBean> findAll() {
        return userMapper.selectList(null);
    }

    @Override
    public boolean add(UserBean user) {
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        qw.eq("r_name",user.getName());

        UserBean tempUser = userMapper.selectOne(qw);
        if(tempUser != null){
            return false;
        }

        userMapper.insert(user);
        return true;
    }

    @Override
    public boolean del(int userId,UserBean loginUser) {
        if(userId == loginUser.getId()){
            return false;
        }

        userMapper.deleteById(userId);

        return true;
    }

    @Override
    public void updateGrade(int userId, String grade) {
        UserBean user = new UserBean();
        user.setId(userId);
        user.setGrade(grade);
        userMapper.updateById(user);
    }

    @Override
    public void updatePwd(int userId, String pwd) {
        UserBean user = new UserBean();
        user.setId(userId);
        user.setPwd(pwd);
        System.out.println(user);
        userMapper.updateById(user);
    }

    @Override
    public UserBean findById(int userId) {
        return userMapper.selectById(userId);
    }
}
